﻿namespace TriangleGames.TrainResEditor
{
    using TriangleGames.TrainRes;
    using System;
    using UnityEngine;

    public static class EditorCommands
    {
        public static void AddGlobalTransitionToSelectedState()
        {
            FsmEditor.Builder.AddGlobalTransition(FsmEditor.SelectedState, null);
        }

        public static void AddState(Vector2 position)
        {
            FsmEditor.SelectState(FsmEditor.Builder.AddState(position), false);
        }

        public static void AddTemplate(object userdata)
        {
            FsmEditor.SelectFsm(FsmBuilder.AddTemplate(userdata as FsmTemplate));
        }

        public static void AddTemplateToSelected(object userdata)
        {
            FsmEditor.SelectFsm(FsmBuilder.AddTemplateToSelected(userdata as FsmTemplate));
        }

        public static void AddTransitionToSelectedState()
        {
            FsmTransition transition = FsmEditor.Builder.AddTransition(FsmEditor.SelectedState);
            FsmEditor.Selection.SelectTransition(transition);
        }

        public static void ChooseWatermark()
        {
            FsmEditor.Inspector.SetMode(InspectorMode.Watermarks);
        }

        public static void CopyFsm()
        {
            FsmBuilder.CopyFsmToClipboard(FsmEditor.SelectedFsm);
        }

        public static void CopyStateSelection()
        {
            FsmBuilder.CopyStatesToClipboard(FsmEditor.SelectedStates);
        }

        public static void DeleteMultiSelection()
        {
            FsmEditor.Builder.DeleteStates(FsmEditor.SelectedStates, false);
            FsmEditor.Selection.DeselectStates();
            if (Event.current != null)
            {
                GUIUtility.ExitGUI();
            }
        }

        public static void DeleteSelectedState()
        {
            FsmState selectedState = FsmEditor.SelectedState;
            FsmEditor.Selection.RemoveState(selectedState);
            FsmEditor.Builder.DeleteState(selectedState, false);
            FsmEditor.Selection.SelectState(null, false, false);
        }

        public static void DeleteSelectedTransition()
        {
            FsmEditor.Builder.DeleteTransition(FsmEditor.SelectedState, FsmEditor.SelectedTransition);
        }

        public static int GetWikiPageNumber(string topic)
        {
            if (Enum.IsDefined(typeof(WikiPages), topic))
            {
                return (int) Enum.Parse(typeof(WikiPages), topic, true);
            }
            return 0;
        }

        public static void OpenWikiHelp()
        {
            OpenWikiPage(WikiPages.Home);
        }

        public static void OpenWikiPage(FsmStateAction action)
        {
            string helpUrlAttribute = FsmEditorUtility.GetHelpUrlAttribute(action.GetType());
            if (helpUrlAttribute != null)
            {
                Application.OpenURL(helpUrlAttribute);
            }
            else if (!OpenWikiPage(FsmEditorUtility.StripNamespace(action.ToString())))
            {
                SearchWikiHelp(action);
            }
        }

        public static void OpenWikiPage(WikiPages page)
        {
            OpenWikiPage((int) page);
        }

        public static void OpenWikiPage(int page)
        {
            Application.OpenURL("https://www.triangle-games.com/default.asp?W" + page);
        }

        public static bool OpenWikiPage(string topic)
        {
            int wikiPageNumber = GetWikiPageNumber(topic);
            if (wikiPageNumber > 0)
            {
                OpenWikiPage(wikiPageNumber);
                return true;
            }
            return false;
        }

        public static void PasteFsm()
        {
            FsmEditor.SelectFsm(FsmBuilder.PasteFsmToSelected());
        }

        public static void PasteStates()
        {
            PasteStates(FsmGraphView.GetViewCenter());
        }

        public static void PasteStates(Vector2 position)
        {
            if (FsmEditor.Builder.CanPaste())
            {
                FsmEditor.Selection.SelectStates(FsmEditor.Builder.PasteStatesFromClipboard(position), false, false);
                FsmEditor.Inspector.ResetView();
                FsmEditor.GraphView.UpdateCanvasSize();
            }
        }

        public static void PasteTemplate(Vector2 position)
        {
            PasteTemplate(FsmEditor.SelectedTemplate, position);
        }

        public static void PasteTemplate(FsmTemplate template, Vector2 position)
        {
            if (template != null)
            {
                FsmEditor.Selection.SelectStates(FsmEditor.Builder.PasteStatesFromTemplate(template, position), false, false);
                FsmEditor.Inspector.ResetView();
                FsmEditor.GraphView.UpdateCanvasSize();
            }
        }

        public static void RemoveFsmComponent()
        {
            if (FsmEditor.SelectedFsm != null)
            {
                FsmEditor.RegisterSceneUndo("Remove FSM Component");
                UnityEngine.Object.DestroyImmediate(FsmEditor.SelectedFsm.Owner, true);
            }
            FsmEditor.RebuildFsmList();
        }

        public static void SaveFsmAsTemplate()
        {
            FsmBuilder.CreateTemplate(FsmEditor.SelectedFsm);
        }

        public static void SaveSelectionAsTemplate()
        {
            FsmBuilder.CreateTemplate(FsmEditor.SelectedStates);
        }

        public static void SearchWikiHelp(FsmStateAction action)
        {
            //Application.OpenURL("https://hutonggames.fogbugz.com/default.asp?ixWiki=1&pg=pgSearchWiki&qWiki=title:" + FsmEditorUtility.NicifyVariableName(FsmEditorUtility.StripNamespace(action.ToString())));
        }

        public static void SearchWikiHelp(string topic)
        {
            //Application.OpenURL("https://hutonggames.fogbugz.com/default.asp?ixWiki=1&pg=pgSearchWiki&qWiki=" + topic);
        }

        public static void SelectAll()
        {
            FsmEditor.Selection.SelectAllStates();
            FsmEditor.Repaint(false);
        }

        public static void SelectFsm(object userdata)
        {
            FsmEditor.SelectFsm((Fsm) userdata);
        }

        public static void SelectState(object userdata)
        {
            FsmEditor.SelectState((FsmState) userdata, true);
        }

        public static void SetSelectedStateAsStartState()
        {
            FsmEditor.Builder.SetStartState(FsmEditor.SelectedState.Name);
        }

        public static void SetSelectedStatesColorIndex(object userdata)
        {
            int colorIndex = (int) userdata;
            FsmBuilder.SetSelectedStatesColorIndex(colorIndex);
        }

        public static void SetStateColorIndex(object userdata)
        {
            int colorIndex = (int) userdata;
            FsmBuilder.SetStateColorIndex(FsmEditor.SelectedState, colorIndex, true);
        }

        public static void SetTransitionColorIndex(object userdata)
        {
            int colorIndex = (int) userdata;
            FsmBuilder.SetTransitionColorIndex(FsmEditor.SelectedFsm, FsmEditor.SelectedTransition, colorIndex, true);
        }

        public static void SetTransitionLinkConstraint(object userdata)
        {
            FsmEditor.Builder.SetLinkConstraint(FsmEditor.SelectedTransition, (FsmTransition.CustomLinkConstraint) userdata);
        }

        public static void SetTransitionLinkStyle(object userdata)
        {
            FsmEditor.Builder.SetLinkStyle(FsmEditor.SelectedTransition, (FsmTransition.CustomLinkStyle) userdata);
        }

        public static void ToggleBreakpointOnSelectedState()
        {
            if (FsmEditor.SelectedState != null)
            {
                FsmEditor.Builder.ToggleBreakpoint(FsmEditor.SelectedState);
            }
        }

        public static void ToggleDebugVariables()
        {
            FsmEditorSettings.DebugVariables = !FsmEditorSettings.DebugVariables;
            FsmEditorSettings.SaveSettings();
        }

        public static void ToggleLogging()
        {
            FsmEditorSettings.EnableLogging = !FsmEditorSettings.EnableLogging;
            FsmLog.LoggingEnabled = FsmEditorSettings.EnableLogging;
            FsmEditorSettings.SaveSettings();
        }

        public static void ToggleSelectNewVariables()
        {
            FsmEditorSettings.SelectNewVariables = !FsmEditorSettings.SelectNewVariables;
            FsmEditorSettings.SaveSettings();
        }
    }
}

